package com.fingard.dsp.bank.reqrespobj.transaction;

import com.fingard.FGBiz;
import com.fingard.dsp.bank.directbank.specialDeal.TransHelper;
import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.dsp.bank.reqrespobj.RetAtsBase;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class RetAtsTrans extends RetAtsBase {
    public String actNum = "";
    public boolean hasSorted = false;
    private boolean isLastSetRespInfo = false;
    private int writeRdCount = 0;
    private List<RetAtsTransDetail> detailList = new ArrayList<RetAtsTransDetail>();
    
    public RetAtsTrans(ReqRespHeader p_header) {
        retHeader = p_header;
        retXmlWriter = new XmlTextWriter();//用于输出返回ATS的XML报文
    }

    public int getWrittenRdCount() {
        return isLastSetRespInfo ? 1 : writeRdCount;
    }
    
    boolean isWritedUseDayNo = false;
    public void writeUseDayNo() {
    	if (!isWritedUseDayNo) {
    		String useDayNo = "";
        	if (FGBiz.dspCenter.qryByEachDay7516) {
        		useDayNo = "1";
        	} else {
        		useDayNo = "0";
        	}
    		retXmlWriter.writeElementString("UseDayNo", useDayNo);
    		isWritedUseDayNo = true;
    	}
	}

    public void writeRetDetail(RetAtsTransDetail p_retDetail) throws Exception {
    	writeUseDayNo();
    	
        if (((FGBiz.dspCenter.enableCacheCurTrans && retHeader.transType.equals("7515")) || (FGBiz.dspCenter.enableCacheHisTrans && retHeader.transType.equals("7516"))) && !hasSorted) {
            isLastSetRespInfo = true;
            detailList.add(p_retDetail); //TransHelper.insertTransCaches(retHeader.transType,p_retDetail);
            return;
        }

        writeRdCount++;
        if (writeRdCount == 1) {
            if (!hasWrittenRespCode) {
                writeRespCode("0");
            }
            if (!hasWrittenRespInfo) {
                writeRespInfo("");
            }
            if (retHeader.transType.equals("7515")) {
                retXmlWriter.writeElementString("ActNum", p_retDetail.actNum);
            }
        }
        retXmlWriter.writeStartElement("DetailRecord");
        if (retHeader.transType.equals("7516")) {
            retXmlWriter.writeElementString("ActArea", p_retDetail.actArea);
            retXmlWriter.writeElementString("ActNum", p_retDetail.actNum);
        }
        retXmlWriter.writeElementString("RecAccNum", p_retDetail.recAccNum);
        retXmlWriter.writeElementString("RecAccName", p_retDetail.recAccName);
        if (retHeader.transType.equals("7515")) {
            retXmlWriter.writeElementString("RecAccBankCode", p_retDetail.recAccBank);
        } else {
            retXmlWriter.writeElementString("RecAccBank", p_retDetail.recAccBank);
        }
        retXmlWriter.writeElementString("CurName", p_retDetail.curName);
        retXmlWriter.writeElementString("TranDate", p_retDetail.tranDate);
        retXmlWriter.writeElementString("TranTime", p_retDetail.tranTime);
        retXmlWriter.writeElementString("ValueDate", p_retDetail.valueDate);
        retXmlWriter.writeElementString("TranAmt", p_retDetail.tranAmt);
        retXmlWriter.writeElementString("CDFlag", p_retDetail.cdFlag);
        retXmlWriter.writeElementString("AccBal", p_retDetail.accBal);
        retXmlWriter.writeElementString("PostTimestamp", p_retDetail.postTimestamp);
        retXmlWriter.writeElementString("BillType", p_retDetail.billType);
        retXmlWriter.writeElementString("ExtraInfo", p_retDetail.extraInfo);
        retXmlWriter.writeElementString("BillNum", p_retDetail.billNum);
        retXmlWriter.writeElementString("Usage", p_retDetail.usage);
        retXmlWriter.writeElementString("Abstr", p_retDetail.abstr);
        retXmlWriter.writeElementString("PayCode", p_retDetail.payCode);
        retXmlWriter.writeElementString("Comment", p_retDetail.comment);
        retXmlWriter.writeElementString("AskNumber", p_retDetail.askNumber);
        retXmlWriter.writeElementString("ReqBatchNO", p_retDetail.reqBatchNO);
        retXmlWriter.writeElementString("Cur", p_retDetail.cur);
        retXmlWriter.writeElementString("SeqId", p_retDetail.seqId);
        retXmlWriter.writeElementString("IsCollection", p_retDetail.isCollection);
        retXmlWriter.writeElementString("Originator", p_retDetail.originator);
        retXmlWriter.writeElementString("BusType", p_retDetail.busType);
        retXmlWriter.writeElementString("URID", p_retDetail.urid);
        retXmlWriter.writeElementString("LowerLevelActNum", p_retDetail.lowerLevelActNum);

        retXmlWriter.writeElementString("GoodsName", p_retDetail.goodsName);
        retXmlWriter.writeElementString("BenefitAmt", p_retDetail.benefitAmt);
        retXmlWriter.writeElementString("RefundAmt", p_retDetail.refundAmt);
        retXmlWriter.writeElementString("RefundBenefitAmt", p_retDetail.refundBenefitAmt);
        retXmlWriter.writeElementString("Fee", p_retDetail.fee);
        retXmlWriter.writeElementString("FeeRate", p_retDetail.feeRate);
        retXmlWriter.writeElementString("OriBizInfo", p_retDetail.oriBizInfo);
        retXmlWriter.writeElementString("OriBizType", p_retDetail.oriBizType);
        retXmlWriter.writeElementString("OriBizTime", p_retDetail.oriBizTime);
        retXmlWriter.writeElementString("Mactibkn", p_retDetail.mactibkn);
        retXmlWriter.writeElementString("Mactbank", p_retDetail.mactbank);
        retXmlWriter.writeElementString("Mactacn", p_retDetail.mactacn);
        retXmlWriter.writeElementString("Mactname", p_retDetail.mactname);

        retXmlWriter.writeElementString("CashPoolBal", p_retDetail.cashPoolBal);
        retXmlWriter.writeElementString("CashPoolAvailBal", p_retDetail.cashPoolAvailBal);
        if ("7515".equals(retHeader.transType)) {
            retXmlWriter.writeElementString("DspUrid", p_retDetail.dspUrid);
        }
        //if (retHeader.transType.equals("7516")) {
        //if(StringHelper.hasAnyChar(p_retDetail.receiptNo)){
        /*if (StringHelper.hasAnyChar(p_retDetail.receiptNo)) {
        	retXmlWriter.writeElementString("ReceiptNo", p_retDetail.receiptNo);
        }else{
        	retXmlWriter.writeElementString("ReceiptNo", p_retDetail.busRef);
        }
        if (StringHelper.hasAnyChar(p_retDetail.busRef)) {
        	retXmlWriter.writeElementString("BusRef", p_retDetail.busRef);
        }else{
        	retXmlWriter.writeElementString("BusRef", p_retDetail.receiptNo);
        }*/
        retXmlWriter.writeElementString("ReceiptNo", p_retDetail.receiptNo);
        retXmlWriter.writeElementString("BusRef", p_retDetail.busRef);
        retXmlWriter.writeElementString("BnkSeqNo", p_retDetail.bnkSeqNo);
        retXmlWriter.writeElementString("ActSeqNo", p_retDetail.actSeqNo);
        //}
        //if(StringHelper.hasAnyChar(p_retDetail.busRef)){
//        retXmlWriter.writeElementString("BusRef", p_retDetail.busRef);
        //}
        //}
        retXmlWriter.writeElementString("UniqueID",p_retDetail.uniqueID);
        retXmlWriter.writeElementString("PossCode",p_retDetail.possCode);
        retXmlWriter.writeEndElement();
    }

    //    RespCode 、RespInfo不在头部返回，需判断所有结果都成功后再赋值
    public void writeRetDetailWithoutRespCode(RetAtsTransDetail p_retDetail) {
        if (((FGBiz.dspCenter.enableCacheCurTrans && retHeader.transType.equals("7515")) || (FGBiz.dspCenter.enableCacheHisTrans && retHeader.transType.equals("7516"))) && !hasSorted) {
            isLastSetRespInfo = true;
            detailList.add(p_retDetail); //TransHelper.insertTransCaches(retHeader.transType,p_retDetail);
            return;
        }

        writeRdCount++;
        if (writeRdCount == 1) {
            if (retHeader.transType.equals("7515")) {
                retXmlWriter.writeElementString("ActNum", p_retDetail.actNum);
            }
        }
        retXmlWriter.writeStartElement("DetailRecord");
        if (retHeader.transType.equals("7516")) {
            retXmlWriter.writeElementString("ActArea", p_retDetail.actArea);
            retXmlWriter.writeElementString("ActNum", p_retDetail.actNum);
        }

        retXmlWriter.writeElementString("RecAccNum", p_retDetail.recAccNum);
        retXmlWriter.writeElementString("RecAccName", p_retDetail.recAccName);
        if (retHeader.transType.equals("7515")) {
            retXmlWriter.writeElementString("RecAccBankCode", p_retDetail.recAccBank);
        } else {
            retXmlWriter.writeElementString("RecAccBank", p_retDetail.recAccBank);
        }
        retXmlWriter.writeElementString("CurName", p_retDetail.curName);
        retXmlWriter.writeElementString("TranDate", p_retDetail.tranDate);
        retXmlWriter.writeElementString("TranTime", p_retDetail.tranTime);
        retXmlWriter.writeElementString("ValueDate", p_retDetail.valueDate);
        retXmlWriter.writeElementString("TranAmt", p_retDetail.tranAmt);
        retXmlWriter.writeElementString("CDFlag", p_retDetail.cdFlag);
        retXmlWriter.writeElementString("AccBal", p_retDetail.accBal);
        retXmlWriter.writeElementString("PostTimestamp", p_retDetail.postTimestamp);
        retXmlWriter.writeElementString("BillType", p_retDetail.billType);
        retXmlWriter.writeElementString("BillNum", p_retDetail.billNum);
        retXmlWriter.writeElementString("Usage", p_retDetail.usage);
        retXmlWriter.writeElementString("Abstr", p_retDetail.abstr);
        retXmlWriter.writeElementString("PayCode", p_retDetail.payCode);
        retXmlWriter.writeElementString("Comment", p_retDetail.comment);
        retXmlWriter.writeElementString("AskNumber", p_retDetail.askNumber);
        retXmlWriter.writeElementString("ReqBatchNO", p_retDetail.reqBatchNO);
        retXmlWriter.writeElementString("Cur", p_retDetail.cur);
        retXmlWriter.writeElementString("SeqId", p_retDetail.seqId);
        retXmlWriter.writeElementString("IsCollection", p_retDetail.isCollection);
        retXmlWriter.writeElementString("Originator", p_retDetail.originator);
        retXmlWriter.writeElementString("BusType", p_retDetail.busType);
        retXmlWriter.writeElementString("URID", p_retDetail.urid);
        retXmlWriter.writeElementString("LowerLevelActNum", p_retDetail.lowerLevelActNum);

        retXmlWriter.writeElementString("GoodsName", p_retDetail.goodsName);
        retXmlWriter.writeElementString("BenefitAmt", p_retDetail.benefitAmt);
        retXmlWriter.writeElementString("RefundAmt", p_retDetail.refundAmt);
        retXmlWriter.writeElementString("RefundBenefitAmt", p_retDetail.refundBenefitAmt);
        retXmlWriter.writeElementString("Fee", p_retDetail.fee);
        retXmlWriter.writeElementString("FeeRate", p_retDetail.feeRate);

        if (retHeader.transType.equals("7515")) {
            retXmlWriter.writeElementString("DspUrid", p_retDetail.dspUrid);
        }
        if (retHeader.transType.equals("7516")) {
            retXmlWriter.writeElementString("ReceiptNo", p_retDetail.receiptNo);
            retXmlWriter.writeElementString("BusRef", p_retDetail.busRef);
        }
        retXmlWriter.writeElementString("UniqueID",p_retDetail.uniqueID);
        retXmlWriter.writeEndElement();
    }

	@Override
    public void writeEnd() {
        Collections.sort(detailList, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                RetAtsTransDetail detail1 = (RetAtsTransDetail) o1;
                RetAtsTransDetail detail2 = (RetAtsTransDetail) o2;
                return detail1.tranTime.compareTo(detail2.tranTime);
            }
        });
        hasSorted = true;

        try {
            for (RetAtsTransDetail detail : detailList) {
                TransHelper.insertTransCaches(retHeader.transType, detail);
                writeRetDetail(detail);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (!hasWrittenRespCode) {
            retXmlWriter.writeElementString("RespCode", "0");
            hasWrittenRespCode = true;
        }
        if (getWrittenRdCount() == 0 && !hasWrittenRespInfo) {
            retXmlWriter.writeElementString("RespInfo", "交易为空");
            hasWrittenRespInfo = true;
        }
        super.writeEnd();
    }

    public void writeEndWithoutInsertDB() {
        super.writeEnd();
    }
}
